package net.xo68.destiny.manager.mapper;


import net.xo68.destiny.manager.entity.JobInstance;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/** 
 * JobInstance mybatis mapper
 * @author wuxie(16349023@qq.com)
 * @date 2020年12月21日 21:43:52
 *
 */
public interface JobInstanceMapper {


	
	@Select("SELECT * FROM destiny_job_instance WHERE instance_id = #{instanceId}")
    @Results(value = { 
    @Result(id = true, column = "instance_id", property = "instanceId"),
        @Result(column = "job_id", property = "jobId"),
        @Result(column = "work_group", property = "workGroup"),
        @Result(column = "job_handler", property = "jobHandler"),
        @Result(column = "sched_host", property = "schedHost"),
        @Result(column = "sched_time", property = "schedTime"),
        @Result(column = "fired_host", property = "firedHost"),
        @Result(column = "fire_time", property = "fireTime"),
        @Result(column = "start_time", property = "startTime"),
        @Result(column = "end_time", property = "endTime"),
        @Result(column = "instance_state", property = "instanceState"),
        @Result(column = "retry_count", property = "retryCount")
    })
    JobInstance find(@Param("instanceId") Long instanceId);
	
	@Select("SELECT * FROM destiny_job_instance")
    @Results(value = { 
    @Result(id = true, column = "instance_id", property = "instanceId"),
        @Result(column = "job_id", property = "jobId"),
        @Result(column = "work_group", property = "workGroup"),
        @Result(column = "job_handler", property = "jobHandler"),
        @Result(column = "sched_host", property = "schedHost"),
        @Result(column = "sched_time", property = "schedTime"),
        @Result(column = "fired_host", property = "firedHost"),
        @Result(column = "fire_time", property = "fireTime"),
        @Result(column = "start_time", property = "startTime"),
        @Result(column = "end_time", property = "endTime"),
        @Result(column = "instance_state", property = "instanceState"),
        @Result(column = "retry_count", property = "retryCount")
    })
    List<JobInstance> list();
	
	@Insert("insert into destiny_job_instance (instance_id ,job_id ,work_group ,job_handler ,sched_host ,sched_time ,fired_host ,fire_time ,start_time ,end_time ,instance_state ,retry_count ) values (#{instanceId} , #{jobId} , #{workGroup} , #{jobHandler} , #{schedHost} , #{schedTime} , #{firedHost} , #{fireTime} , #{startTime} , #{endTime} , #{instanceState} , #{retryCount}  )")
    @Options(useGeneratedKeys = true, keyProperty = "instanceId")
    int insert(JobInstance jobInstance);
	
	@Update({"update destiny_job_instance set",
      " job_id=#{jobId}, work_group=#{workGroup}, job_handler=#{jobHandler}, sched_host=#{schedHost}, sched_time=#{schedTime}, fired_host=#{firedHost}, fire_time=#{fireTime}, start_time=#{startTime}, end_time=#{endTime}, instance_state=#{instanceState}, retry_count=#{retryCount} ",
      "where instance_id = #{instanceId}"
    })
    int update(JobInstance jobInstance);

    @Update({"update destiny_job_instance set",
            " sched_host=#{schedHost}, sched_time=#{schedTime}, fired_host=#{firedHost},instance_state=#{instanceState}",
            "where instance_id = #{instanceId}"
    })
    int sched(@Param("instanceId") Long instanceId,@Param("schedHost") String schedHost,@Param("schedTime") Date schedTime,@Param("firedHost") String firedHost,@Param("instanceState") Byte instanceState);



    @Update({"update destiny_job_instance set",
            "  start_time=#{startTime}",
            "where instance_id = #{instanceId}"
    })
    int handleStart(@Param("instanceId") Long instanceId,@Param("startTime") Date startTime);

    @Update({"update destiny_job_instance set",
            "  instance_state=#{instanceState},end_time=#{endTime}",
            "where instance_id = #{instanceId}"
    })
    int handleFinish(@Param("instanceId") Long instanceId,@Param("instanceState") Byte instanceState,@Param("endTime") Date endTime);

	@Delete("delete FROM destiny_job_instance WHERE instance_id = #{instanceId}")
	int delete(@Param("instanceId") Long instanceId);
}